<?php
namespace app\api\controller\wdsxh\single;
use app\api\model\wdsxh\mall\Cart;
use app\common\controller\Api;
/**
 * 商品列表
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //商品列表
    public function list(){
        $param = $this->request->param();
        $where = [];
        $where['status'] = array('eq','normal');
        //单商城 = 1&&多商城 = 2
        if(isset($param['source']) && !empty($param['source'])) {
            $where['source'] = array('eq',$param['source']);
        }
        //单商城
        //搜索商品
        if(isset($param['keywords']) && !empty($param['keywords'])) {
            $where['name'] = array('like','%'.$param['keywords'].'%');
        }
        //分类 (单商城 && 多商城)
        if(isset($param['category_id']) && !empty($param['category_id'])) {
            $where['category_id'] = array('eq',$param['category_id']);
        }
        //是否热销
        if(isset($param['is_hot']) && !empty($param['is_hot'])) {
            $where['is_hot'] = array('eq',$param['is_hot']);
        }
        //是否最新
        if(isset($param['is_new']) && !empty($param['is_new'])) {
            $where['is_new'] = array('eq',$param['is_new']);
        }
        //是否推荐
        if(isset($param['is_recommend']) && !empty($param['is_recommend'])) {
            $where['is_recommend'] = array('eq',$param['is_recommend']);
        }
        //多商城
        //商品排序
         $order = 'weigh desc,id desc';
        if (isset($param['source']) && !empty($param['source']) && $param['source'] == 2) {
            $order = 'is_hot desc,weigh desc,id desc';
        }
        if(isset($param['store_id']) && !empty($param['store_id'])) {
            $where['store_id'] = array('eq',$param['store_id']);
        }
        $page = isset($param['page']) ? $param['page'] : '';
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $count = (new \app\admin\model\wdsxh\mall\Goods())
            ->where($where)
            ->count();
        $data = (new \app\admin\model\wdsxh\mall\Goods())
            ->where($where)
            ->page($page,$limit)
            ->order($order)
            ->field('id,name,image,price,recommend_image,is_hot')
            ->select();
        $this->success('请求成功',['count' => $count,'data'=> $data]);
    }

    //商品详情
    public function details(){
        $id = $this->request->param('id');
        $data = (new \app\admin\model\wdsxh\mall\Goods())
            ->where('id',$id)
            ->field('name,image,recommend_image,slider_images,price,ot_price,param_json,content')
            ->find();
        $value_array = explode(',', $data['slider_images']);
        $result = [];
        foreach ($value_array as $v) {
            $result[] = wdsxh_full_url($v);
        }
        $data['slider_images'] = $result;
        $this->success('请求成功',$data);
    }

    public function cart_count(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $cartWhere['cart.user_id'] = array('eq',$user_id);
        $cartWhere['cart.buy_now'] = array('eq','2');
        $cartWhere['cart.paid'] = array('eq','0');
        $data = (new Cart())
            ->alias('cart')
            ->where($cartWhere)
            ->order('cart.createtime desc,cart.id desc')
            ->join('wdsxh_mall_goods goods','goods.id = cart.goods_id')
            ->join('wdsxh_mult_store store','cart.store_id = store.id','left')
            ->field('cart.id cart_id,cart.goods_id,cart.goods_num,cart.store_id,
            goods.name goods_name,goods.image goods_image,goods.price goods_price,
            store.name store_name,store.image store_image')
            ->count();
        $this->success('请求成功',['cart_count'=>$data]);
    }


}